#coding-utf-8
#@Time    : 2019/3/12 21:31
#@Author  : 刘云
#@Email   : wnliuyun@126.com
#@File    : week3.py
#@Software: PyCharm

import datetime
import cv2
import time
import numpy as np


colour=((0, 205, 205),(154, 250, 0),(34,34,178),(211, 0, 148),(255, 118, 72),(137, 137, 139))#定义矩形颜色
cap = cv2.VideoCapture("E:\\opencv\\sources\\samples\\data\\vtest.avi") #参数为0是打开摄像头，文件名是打开视频
fgbg = cv2.createBackgroundSubtractorMOG2()#混合高斯背景建模算法
fourcc = cv2.VideoWriter_fourcc(*'XVID')#设置保存图片格式
out = cv2.VideoWriter('test.avi',fourcc, 10.0, (768,576))#分辨率要和原视频对应

while True:
    ret, frame = cap.read()  # 读取图片
    fgmask = fgbg.apply(frame)
    if fgmask is None:
        break
    element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))  # 形态学去噪
    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, element)  # 开运算去噪
    # fgmask = cv2.GaussianBlur(fgmask, (5, 5), 0) #高斯滤波
    _, contours, hierarchy = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 寻找前景
    count = 0
    for cont in contours:
        Area = cv2.contourArea(cont)  # 计算轮廓面积
        if Area < 300:  # 过滤面积小于10的形状
            continue
        count += 1  # 计数加一
        print("{}-prospect:{}".format(count, Area), end="  ")  # 打印出每个前景的面积
        rect = cv2.boundingRect(cont)  # 提取矩形坐标
        print("x:{} y:{}".format(rect[0], rect[1]))  # 打印坐标
        cv2.rectangle(frame, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), colour[count % 6],1)  # 原图上绘制矩形
        # cv2.rectangle(fgmask, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0xff, 0xff, 0xff),1)  # 黑白前景上绘制矩形
        y = 10 if rect[1] < 10 else rect[1]  # 防止编号到图片之外
        cv2.putText(frame, str(count), (rect[0], y), cv2.FONT_HERSHEY_COMPLEX, 0.4, (0, 255, 0), 1)  # 在前景上写上编号
    cv2.putText(frame, "count:", (5, 20), cv2.FONT_HERSHEY_COMPLEX, 0.6, (0, 255, 0), 1)  # 显示总数
    # cv2.putText(frame, str(count), (75, 20), cv2.FONT_HERSHEY_COMPLEX, 0.6, (0, 255, 0), 1)
    print("----------------------------")
    cv2.imshow('frame', frame)  # 在原图上标注
    cv2.imshow('frame2', fgmask)  # 以黑白的形式显示前景和背景
    out.write(frame)
    k = cv2.waitKey(30) & 0xff  # 按esc退出
    if k == 27:
        break


out.release()#释放文件
cap.release()
cv2.destroyAllWindows()#关闭所有窗口




